<!DOCTYPE html>
<html>

<head>
    <link rel="stylesheet" href="../../plugins/layui/css/layui.css"/>
    <link rel="stylesheet" href="../../css/normalize.css"/>
    <link rel="stylesheet" href="../../css/fa.css"/>
    <link rel="stylesheet" href="../../css/icon.css"/>
    <link rel="stylesheet" href="../../css/ares_animate.min.css"/>
    <link rel="stylesheet" href="../../css/ares_hover_variant.min.css"/>
    <link rel="stylesheet" href="../../css/ares_reboot.css"/>
    <link rel="stylesheet" href="../../css/app.min.css"/>
    <link rel="stylesheet" href="../../css/iframe.css"/>

</head>

<body>
<!--<div class="ares-text-right">
    <button id="his-btn" class="layui-hide layui-btn" onclick="">查看HIS系统病例</button>
    <button id="tar-btn" class="layui-hide layui-btn" onclick="referral_history();">查看转诊历史</button>
</div>-->
<h3>基本信息</h3>
<div class="ares-mb emr-info ares-border-bottom"></div>

<div class="ares-clearfix">
    <button id="clinic-btn" class="layui-btn layui-hide ares-float-right">新建</button>
    <h3>病历记录</h3>
    <table class="layui-hide" id="clinic" lay-filter="clinic"></table>
</div>

<div>
    <h3>会诊记录</h3>
    <table class="layui-hide" id="consultation" lay-filter="consultation"></table>
</div>

<div>
    <button id="evaluate-btn" class="layui-btn layui-hide ares-float-right">新建</button>
    <h3>评估处方</h3>
    <table class="layui-hide" id="evaluate" lay-filter="evaluate"></table>
</div>

<div>
    <button id="training-prcp-btn" class="layui-btn layui-hide ares-float-right">新建</button>
    <h3>训练处方</h3>
    <table class="layui-hide" id="training-prcp" lay-filter="training-prcp"></table>
</div>

<div>
    <button id="training-plan-btn" class="layui-btn layui-hide ares-float-right">新建</button>
    <h3>康复训练方案</h3>
    <table class="layui-hide" id="training-plan" lay-filter="training-plan"></table>
</div>

<div>
    <h3>治疗记录</h3>
    <table class="layui-hide" id="treatment-record" lay-filter="treatment-record"></table>
</div>

<div>
    <button id="inspection-report-btn" class="layui-btn layui-hide ares-float-right">上传</button>
    <h3>检查报告</h3>
    <table class="layui-hide" id="inspection-report" lay-filter="inspection-report"></table>
</div>


<script type="text/html" id="clinic-bar">
    <button class="layui-btn layui-btn-xs" lay-event="check">查看</button>
    {{#  if(d.editAble && d.isNew){ }}
    <button class="layui-btn layui-btn-xs" lay-event="update">更新</button>
    {{#  } }}

</script>
<script type="text/html" id="consultation-bar">
    <button class="layui-btn layui-btn-xs" lay-event="check">查看</button>
</script>
<script type="text/html" id="evaluate-bar">
    <button class="layui-btn layui-btn-xs" lay-event="check">查看</button>
</script>
<script type="text/html" id="training-prcp-bar">
    <button class="layui-btn layui-btn-xs" lay-event="check">查看</button>
</script>
<script type="text/html" id="training-plan-bar">
    <button class="layui-btn layui-btn-xs" lay-event="check">查看</button>
</script>
<script type="text/html" id="treatment-record-bar">
    <button class="layui-btn layui-btn-xs" lay-event="check">查看</button>
</script>
<script type="text/html" id="inspection-report-bar">
    <button class="layui-btn layui-btn-xs" lay-event="check">查看</button>
</script>
<script type="text/javascript" src="../../plugins/layui/layui.js"></script>
<script type="text/javascript" src="../../aresExtends/config/layuiConfig.js"></script>
<script>
    var referral_history, init;
    layui.use(['table', 'utils', 'filters', 'dateFilters', 'greyServices', 'mandyServices', 'store', 'qs', 'widget'], function (table, utils, filters, dateFilters, greyServices, mandyServices, store, qs, widget) {
        var routerParams = {},
            routerQuery = {};

        var currJob = store.session.get('job'),
            currHospitalId = store.session.get('hospital');

        /* var $hisBtn = $("#his-btn"),
             $tarBtn = $("#tar-btn");*/

        var $clinicBtn = $('#clinic-btn'),
            $evaluateBtn = $('#evaluate-btn'),
            $trainingPrcpBtn = $('#training-prcp-btn'),
            $trainingPlanBtn = $('#training-plan-btn'),
            $inspectionReportBtn = $('#inspection-report-btn');


        var patientId = "",
            hospitalId = null,
            treatGroupName = null,
            newestPrcpItem = {};


        init = function () {
            var localHash = location.hash,
                queryStr = localHash.split('?')[1];
            routerParams = top.layui.routers.params;
            routerQuery = queryStr ? qs.parse(queryStr) : {};
            //getTreatGroup()
            getEmrInfo()
            loadEvaluateList(routerParams.emrId);
            loadTrainingPrcpList(routerParams.emrId);
            loadTrainingPlanList();
            loadingTrainingRecordList();
            loadingClinicList();
            loadingConsultationList();
        }
        referral_history = function () {
            var query = {
                transferId: routerQuery.transferId
            };
            utils.pushTopRoute('/referral/history/' + routerParams.emrId, query);
        }

        init();

        $clinicBtn.on('click', newClinic)
        $evaluateBtn.on('click', newEvaluate)
        $trainingPrcpBtn.on('click', newTrainingPrcp)
        $trainingPlanBtn.on('click', newTrainingPlan)
        $inspectionReportBtn.on('click', addReport)

        function getTreatGroup() {
            mandyServices.mandy_get_my_treat_group_syn().then(function (res) {
                treatGroupName = res.data ? res.data.name : '';
            })
        }

        function getEmrInfo() {
            mandyServices.mandy_get_emr(routerParams.emrId, routerQuery.transferId, routerQuery.idCard).then(function (res) {
                var data = res.data;
                patientId = data.patient.id;
                hospitalId = data.hospital.id;
                widget.createPatientBaseInfo($('.emr-info'),data);
                /*if (data.classify == "null" || data.classify == null) {
                    $hisBtn.addClass("layui-hide");
                } else {
                    $hisBtn.removeClass("layui-hide");
                }
                if (data.hasRefHis && !routerQuery.mesEmr) {
                    $tarBtn.removeClass("layui-hide");
                } else {
                    $tarBtn.addClass("layui-hide");
                }*/
                loadingReport();
                showBtn();
            });
        }

        function showBtn() {
            if (currJob == "doctor" && hospitalId == currHospitalId) {
                $clinicBtn.removeClass("layui-hide");
                $evaluateBtn.removeClass("layui-hide");
                $trainingPrcpBtn.removeClass("layui-hide");
                $inspectionReportBtn.removeClass("layui-hide");
            } else if (currJob == "therapist" && hospitalId == currHospitalId) {
                mandyServices.mandy_get_newest_prcp_item(routerParams.emrId, function (res) {
                    var data = res.data;
                    newestPrcpItem = data;
                    $trainingPlanBtn.removeClass("layui-hide");
                });
            }
        }

        function loadEvaluateList(emrId) {
            greyServices.grey_get_prescription_by_emr_id(emrId, null, "emr_self").then(function (res) {
                var data = res.data;
                var arr = data.prescription_list || [];
                var list = [];
                $.each(arr, function (idx, item) {
                    if (item.type == 1) list.push(item);
                });
                table.render({
                    elem: '#evaluate',
                    data: list,
                    cellMinWidth: 80,
                    cols: [[
                        {title: '文档名称', templet: '<span>评估处方</span>'},
                        {title: '创建人', templet: '<span>{{d.creator.name}}</span>'},
                        {
                            title: '记录时间',
                            templet: function (d) {
                                return dateFilters.dateTimeFormat(d.create_time);
                            }
                        },
                        {title: '操作', fixed: 'right', width: 100, templet: '#evaluate-bar'}
                    ]]
                });
                table.on('tool(evaluate)', function (obj) {
                    var data = obj.data;
                    switch (obj.event) {
                        case 'check':
                            openEvaluateDetail(data)
                            break;
                    }
                    ;
                });
            });
        }

        function newEvaluate() {
            utils.pushTopRoute('/evaluation/create/' + routerParams.emrId);
        }

        function openEvaluateDetail(data) {
            var query = {
                isReferralHistory: 'emr_self',
            };
            utils.pushTopRoute('/evaluation/detail/' + routerParams.emrId + '/' + data.id, query);
        }

        function loadTrainingPrcpList(emrId) {
            greyServices.grey_get_prescription_by_emr_id(emrId, null, "emr_self").then(function (res) {
                var data = res.data;
                var arr = data.prescription_list || [];
                var list = [];
                $.each(arr, function (idx, item) {
                    if (item.type == 2) list.push(item);
                });
                table.render({
                    elem: '#training-prcp',
                    data: list,
                    cellMinWidth: 80,
                    cols: [[
                        {title: '文档名称', templet: '<span>训练处方</span>'},
                        {title: '创建人', templet: '<span>{{d.creator.name}}</span>'},
                        {
                            title: '记录时间',
                            templet: function (d) {
                                return dateFilters.dateTimeFormat(d.create_time);
                            }
                        },
                        {title: '操作', fixed: 'right', width: 100, templet: '#training-prcp-bar'}
                    ]]
                });
                table.on('tool(training-prcp)', function (obj) {
                    var data = obj.data;
                    switch (obj.event) {
                        case 'check':
                            openPrcpDetail(data)
                            break;
                    }
                    ;
                });
            });
        }

        function newTrainingPrcp() {
            utils.pushTopRoute('/training/create/' + routerParams.emrId);
        }

        function openPrcpDetail(data) {
            var query = {
                selectPrcpId: data.id,
                isReferralHistory: 'emr_self',
            };
            utils.pushTopRoute('/training/detail/' + routerParams.emrId, query);
        }

        function lookTrainingPlan(data) {
            var query = {
                isReferralHistory: 'emr_self',
                itemType: data.prescription_item.prcp_item_type.name,
            };
            utils.pushTopRoute('/training/plan/' + routerParams.emrId, query);
        }

        function newTrainingPlan() {
            var query = {
                myParam1: 'old',
                myParam2: 'add',
                myParam3: 'type',
                isReferralHistory: 'emr_self',
                item_id: newestPrcpItem.list[0].id,
                itemType: newestPrcpItem.list[0].prcp_item_type.name,
                transferId: routerQuery.transferId,
                isGeneral: treatGroupName == '全科治疗组' ? 1 : 0
            };
            utils.pushTopRoute('/training/plan/' + routerParams.emrId, query);
        }

        function loadTrainingPlanList() {
            mandyServices.mandy_get_newest_training_plan(routerParams.emrId, "emr_self").then(function (res) {
                var data = res.data,
                    list = data.list || [];
                table.render({
                    elem: '#training-plan',
                    data: list,
                    cellMinWidth: 80,
                    cols: [[
                        {title: '文档名称', templet: '<span>{{d.prescription_item.prcp_item_type.name}}</span>'},
                        {title: '创建人', templet: '<span>{{d.creator.name}}</span>'},
                        {
                            title: '记录时间',
                            templet: function (d) {
                                return dateFilters.dateTimeFormat(d.createTime);
                            }
                        },
                        {title: '操作', fixed: 'right', width: 100, templet: '#training-plan-bar'}
                    ]]
                });
                table.on('tool(training-plan)', function (obj) {
                    var data = obj.data;
                    switch (obj.event) {
                        case 'check':
                            lookTrainingPlan(data)
                            break;
                    }
                    ;
                });
            })
        }

        function lookTreatmentRecord(data) {
            var query = {
                isReferralHistory: 'emr_self',
                itemId: data.prescription_item.id,
                itemType: data.prescription_item.prcp_item_type.name,
                transferId: routerQuery.transferId
            };
            utils.pushTopRoute('/training/record/' + routerParams.emrId, query);
        }

        function loadingTrainingRecordList() {
            mandyServices.mandy_get_newest_treatment_records(routerParams.emrId, "emr_self").then(function (res) {
                var data = res.data,
                    list = data.list || [];
                table.render({
                    elem: '#treatment-record',
                    data: list,
                    cellMinWidth: 80,
                    cols: [[
                        {title: '文档名称', templet: '<span>{{d.prescription_item.prcp_item_type.name}}</span>'},
                        {title: '创建人', templet: '<span>{{d.therapist.name}}</span>'},
                        {
                            title: '记录时间',
                            templet: function (d) {
                                return dateFilters.dateTimeFormat(d.finish_time);
                            }
                        },
                        {title: '操作', fixed: 'right', width: 100, templet: '#treatment-record-bar'}
                    ]]
                });
                table.on('tool(treatment-record)', function (obj) {
                    var data = obj.data;
                    switch (obj.event) {
                        case 'check':
                            lookTreatmentRecord(data)
                            break;
                    }
                    ;
                });
            });
        }

        function loadingClinicList() {
            mandyServices.mandy_get_clinic_list(routerParams.emrId, "emr_self", 1, 10000).then(function (res) {
                var data = res.data,
                    list = data.list || [];
                $.each(list, function (idx, item) {
                    item.editAble = !!routerQuery.disEdit;
                    item.isNew = utils.newHospitalized(item.create_time) <= 7;
                });
                table.render({
                    elem: '#clinic',
                    data: list,
                    cellMinWidth: 80,
                    cols: [[
                        {title: '文档名称', templet: '<span>病历记录</span>'},
                        {
                            title: '病历号',
                            templet: function (d) {
                                var info = JSON.parse(d.disease_info)
                                return info.patientNumber || info.hospitalNumber || "";
                            }
                        },
                        {title: '创建人', templet: '<span>{{d.creator.name}}</span>'},
                        {
                            title: '记录时间',
                            templet: function (d) {
                                return dateFilters.dateTimeFormat(d.create_time);
                            }
                        },
                        {title: '操作', fixed: 'right', width: 100, templet: '#clinic-bar'}
                    ]]
                });
                table.on('tool(clinic)', function (obj) {
                    var data = obj.data;
                    switch (obj.event) {
                        case 'check':
                            lookClinic(data)
                            break;
                        case 'update':
                            editClinic(data)
                            break;
                    }
                    ;
                });
            });
        }

        function newClinic() {
            var query = {
                readonly: false
            }
            utils.pushTopRoute('/emr/clinicDetail/' + routerParams.emrId, query);
        }

        function lookClinic(data) {
            var query = {
                isReferralHistory: 'emr_self',
                transferId: routerQuery.transferId,
                clinicId: data.id
            };
            utils.pushTopRoute('/emr/clinicDetail/' + routerParams.emrId, query);
        }

        function editClinic(data) {
            var query = {
                isReferralHistory: 'emr_self',
                transferId: routerQuery.transferId,
                edit: 1,
                readonly: false,
                clinicId: data.id
            };
            utils.pushTopRoute('/emr/clinicDetail/' + routerParams.emrId, query);
        }

        function loadingReport() {
            mandyServices.mandy_get_inspection_report_list(patientId, routerParams.emrId, "emr_self").then(function (res) {
                    var data = res.data,
                        list = data.inspection_report_list || [];
                    table.render({
                        elem: '#inspection-report',
                        data: list,
                        cellMinWidth: 80,
                        cols: [[
                            {title: '文档名称', templet: '<span>检查报告</span>'},
                            {title: '创建人', templet: '<span>{{d.creator.name}}</span>'},
                            {
                                title: '记录时间',
                                templet: function (d) {
                                    return dateFilters.dateTimeFormat(d.check_time);
                                }
                            },
                            {title: '操作', fixed: 'right', width: 100, templet: '#inspection-report-bar'}
                        ]]
                    });
                    table.on('tool(inspection-report)', function (obj) {
                        var data = obj.data;
                        switch (obj.event) {
                            case 'check':
                                lookReport(data)
                                break;
                        }
                        ;
                    });
                }
            );
        }

        function addReport() {
            var query = {
                isReferralHistory: 'emr_self',
            };
            utils.pushTopRoute('/report/create/' + routerParams.emrId, query);
        }

        function lookReport(data) {
            var query = {
                isReferralHistory: 'emr_self',
                transferId: routerQuery.transferId
            };
            utils.pushTopRoute('/report/detail/' + routerParams.emrId + '/' + data.id, query);
        }

        /**
         * @author:ares
         * @datetime:2019/3/18 16:57
         * @description: 加载会诊记录
         */
        function loadingConsultationList() {
            mandyServices.mandy_get_consultation_list(routerParams.emrId, 1, 10000).then(function (res) {
                var data = res.data,
                    list = data.list || [];

                table.render({
                    elem: '#consultation',
                    data: list,
                    cellMinWidth: 80,
                    cols: [[
                        {title: '文档名称', templet: '<span>会诊记录</span>'},
                        {title: '创建人', templet: '<span>{{d.doctorName}}</span>'},
                        {
                            title: '记录时间',
                            templet: function (d) {
                                return dateFilters.dateTimeFormat(d.create_time);
                            }
                        },
                        {title: '操作', fixed: 'right', width: 100, templet: '#consultation-bar'}
                    ]]
                });
                table.on('tool(consultation)', function (obj) {
                    var data = obj.data;
                    switch (obj.event) {
                        case 'check':
                            lookConsultation(data)
                            break;
                    }
                    ;
                });
            });
        }

        function lookConsultation(data) {
            utils.layerOpen({
                type: 2,
                title: '查看会诊详情',
                area: ['800px', '500px'],
                content: 'pages/consultation/detail.html?id=' + data.id,
                end: function (index, layero) {

                }
            });
        }

    })
</script>

</body>
</html>



